package net.jgrm.order.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import net.jgrm.order.dto.CreateOrderRequest;
import net.jgrm.order.entity.ReceiverInfo;

/**
 * PostgreSQL pgcrypto加密查询功能测试数据生成器
 * 用于生成测试PostgreSQL原生加密存储和查询功能的示例数据
 */
public class PgCryptoTestGenerator {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) throws Exception {
        System.out.println("=== PostgreSQL pgcrypto加密存储与查询测试 ===");

        // 测试数据
        generateTestOrderData();

        System.out.println("\n=== 加密查询接口测试 ===");
        generateSearchTestCases();

        System.out.println("\n=== 数据库功能验证SQL ===");
        generateTestSQL();
    }

    private static void generateTestOrderData() throws Exception {
        System.out.println("\n1. 创建订单测试数据:");

        // 测试订单1
        CreateOrderRequest request1 = new CreateOrderRequest();
        request1.setUserId(1001L);
        request1.setProductId(1L);
        request1.setQuantity(2);

        ReceiverInfo receiver1 = new ReceiverInfo();
        receiver1.setName("张三");
        receiver1.setPhone("13800138000");
        receiver1.setProvince("广东省");
        receiver1.setCity("深圳市");
        receiver1.setDistrict("南山区");
        receiver1.setAddress("科技园南区深圳湾科技生态园10栋A座");
        request1.setReceiverInfo(receiver1);

        System.out.println("订单1 (张三):");
        System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(request1));

        // 测试订单2
        CreateOrderRequest request2 = new CreateOrderRequest();
        request2.setUserId(1002L);
        request2.setProductId(2L);
        request2.setQuantity(1);

        ReceiverInfo receiver2 = new ReceiverInfo();
        receiver2.setName("李四");
        receiver2.setPhone("13900139000");
        receiver2.setProvince("北京市");
        receiver2.setCity("北京市");
        receiver2.setDistrict("朝阳区");
        receiver2.setAddress("三里屯SOHO 2号楼");
        request2.setReceiverInfo(receiver2);

        System.out.println("\n订单2 (李四):");
        System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(request2));
    }

    private static void generateSearchTestCases() {
        System.out.println("\n2. 按收货人姓名查询订单:");
        System.out.println("GET /api/orders/search/by-receiver-name?receiverName=张三");
        System.out.println("GET /api/orders/search/by-receiver-name?receiverName=李四");

        System.out.println("\n3. 按收货人手机号查询订单:");
        System.out.println("GET /api/orders/search/by-receiver-phone?receiverPhone=13800138000");
        System.out.println("GET /api/orders/search/by-receiver-phone?receiverPhone=13900139000");
    }

    private static void generateTestSQL() {
        System.out.println("\n4. 直接数据库验证SQL:");

        System.out.println("\n-- 验证pgcrypto扩展是否启用");
        System.out.println("SELECT * FROM pg_extension WHERE extname = 'pgcrypto';");

        System.out.println("\n-- 查看订单表结构");
        System.out.println("\\d t_order");

        System.out.println("\n-- 查看加密存储的数据");
        System.out.println("SELECT id, order_no, receiver_name_hash, receiver_phone_hash, receiver_province, receiver_city FROM t_order;");

        System.out.println("\n-- 使用pgcrypto解密查看数据（需要密钥）");
        System.out.println("SELECT id, order_no,");
        System.out.println("  pgp_sym_decrypt(receiver_name::bytea, 'MySecretKey12345') as decrypted_name,");
        System.out.println("  pgp_sym_decrypt(receiver_phone::bytea, 'MySecretKey12345') as decrypted_phone,");
        System.out.println("  receiver_province, receiver_city");
        System.out.println("FROM t_order;");

        System.out.println("\n-- 测试hash索引查询");
        System.out.println("-- 需要在应用中生成hash值，例如:");
        System.out.println("-- String nameHash = aesEncryptionUtil.generateDeterministicHash(\"张三\");");
        System.out.println("-- 然后使用该hash值查询:");
        System.out.println("-- SELECT * FROM t_order WHERE receiver_name_hash = '计算出的hash值';");
    }
}